// index.js
import request from '@/api/request.ts';
import { v4 as uuidv4 } from 'uuid';
import { createParams,generateUUID } from '@/utils/utils.js'

let userInfo = uni.getStorageSync('USERINFO') ;

export const postLogin = (data = {}) => {
	// return request.post('/doLogin', data)
	return new Promise((resolve, reject) => {
	    uni.getStorage({
	        key: 'HTTP',
	        success: function (storageRes) {
	            // 假设 createParams 是一个同步函数，你可以直接传入 storageRes.data
				console.log(storageRes.data);
	            request.post(`${storageRes.data}/doLogin`, Object.assign(data))
	                .then(response => {
	                    resolve(response);
	                })
	                .catch(error => {
	                    reject(error);
	                });
	        },
	        fail: function (error) {
	            reject(error);
	        }
	    });
	  });
}

export const postUserInfo = (data = {}) => {
	// return request.post('/doUserInfo', data)
	return new Promise((resolve, reject) => {
	    uni.getStorage({
	        key: 'HTTP',
	        success: function (storageRes) {
	            // 假设 createParams 是一个同步函数，你可以直接传入 storageRes.data
				console.log(storageRes.data);
	            request.post(`${storageRes.data}/doUserInfo`, Object.assign(data))
	                .then(response => {
	                    resolve(response);
	                })
	                .catch(error => {
	                    reject(error);
	                });
	        },
	        fail: function (error) {
	            reject(error);
	        }
	    });
	  });
}

export const postCall = (data = {}) => {
	
	//  return new Promise((resolve, reject) => {
	//     var keys = ['HTTP', 'USERINFO'];
	//     var promises = keys.map(key => {
	//       return new Promise((resolve, reject) => {
	//         uni.getStorage({
	//           key: key,
	//           success: function (res) {
	//             resolve({ key: key, value: res.data });
	//           },
	//           fail: function (error) {
	//             reject(error);
	//           }
	//         });
	//       });
	//     });
	
	//     Promise.all(promises)
	//       .then(results => {
	//         var datas = {};
	//         results.forEach(result => {
	//           datas[result.key] = result.value;
	//         });
	//         // resolve(data);
	// 		data["P_SIGN"] = uuidv4();
	// 		const params = createParams(datas.USERINFO);
	// 		console.log(params)
	// 		request.post(`${datas.HTTP}/call`, Object.assign(data, params))
	//       })
	//       .catch(error => {
	//         reject(error);
	//       });
	//   });
	
	// data["P_SIGN"] = uuidv4();
	// return request.post('/call', Object.assign(data,createParams(userInfo)))  
	// const httpStr = uni.getStorageSync('HTTP') ;  
	// return new Promise((resolve, reject) => {
	//     uni.getStorage({
	//         key: 'USERINFO',
	//         success: function (storageRes) {
	//             data["P_SIGN"] = uuidv4();
	//             // 假设 createParams 是一个同步函数，你可以直接传入 storageRes.data
	//             const params = createParams(storageRes.data);
	//             request.post(`${httpStr}/call`, Object.assign(data, params))
	//                 .then(response => {
	//                     resolve(response);
	//                 })
	//                 .catch(error => {
	//                     reject(error);
	//                 });
	//         },
	//         fail: function (error) {
	//             reject(error);
	//         }
	//     });
	//   });
	return new Promise((resolve, reject) => {
	  uni.getStorage({
	    key: 'HTTP',
	    success: function (res) {
	      const httpStr = res.data;
	      resolve(httpStr);
	    },
	    fail: function (error) {
	      reject(error);
	    }
	  });
	})
	  .then(httpStr => {
	    return new Promise((resolve, reject) => {
	      uni.getStorage({
	        key: 'USERINFO',
	        success: function (storageRes) {
	          data["P_SIGN"] = generateUUID();
	          // 假设 createParams 是一个同步函数，你可以直接传入 storageRes.data
	          const params = createParams(storageRes.data);
	          request.post(`${httpStr}/call`, Object.assign(params,data) ,{} , data.isShowLoading === false ? false : true  )
	            .then(response => {
	              resolve(response);
	            })
	            .catch(error => {
	              reject(error);
	            });
	        },
	        fail: function (error) {
	          reject(error);
	        }
	      });
	    });
	  })
	  .catch(error => {
	    console.error(error);
		if(error.errMsg?.includes("fail")){
			uni.navigateTo({
				url: '/pages/login/login'
			});
		}
		
	  });
}

export const postNoCall = (data = {}) => {
	// data["P_SIGN"] = uuidv4();
	// return request.post('/call', Object.assign(data,createParams(userInfo)))
	return new Promise((resolve, reject) => {
	    uni.getStorage({
	        key: 'HTTP',
	        success: function (storageRes) {
	            data["P_SIGN"] = generateUUID();
	            // 假设 createParams 是一个同步函数，你可以直接传入 storageRes.data
	            // const params = createParams(storageRes.data);
	            request.post(`${storageRes.data}/noCall`, data)
	                .then(response => {
	                    resolve(response);
	                })
	                .catch(error => {
	                    reject(error);
	                });
	        },
	        fail: function (error) {
	            reject(error);
	        }
	    });
	  });
}

export const postSendCaptcha = (data = {}) => {
	// data["P_SIGN"] = uuidv4();
	// return request.post('/call', Object.assign(data,createParams(userInfo)))
	return new Promise((resolve, reject) => {
	    uni.getStorage({
	        key: 'HTTP',
	        success: function (storageRes) {
	            data["P_SIGN"] = generateUUID();
	            // 假设 createParams 是一个同步函数，你可以直接传入 storageRes.data
	            const params = createParams(storageRes.data);
	            request.post(`${storageRes.data}/sendCaptcha`, Object.assign( params,data))
	                .then(response => {
	                    resolve(response);
	                })
	                .catch(error => {
	                    reject(error);
	                });
	        },
	        fail: function (error) {
	            reject(error);
	        }
	    });
	  });
}

export const postPay = (data = {}) => {
	// return request.post('/tonglianPayGetSign', data)
	
	return new Promise((resolve, reject) => {
	    uni.getStorage({
	        key: 'HTTP',
	        success: function (storageRes) {
	            request.post(`${storageRes.data}/tonglianPayGetSign`, Object.assign(data))
	                .then(response => {
	                    resolve(response);
	                })
	                .catch(error => {
	                    reject(error);
	                });
	        },
	        fail: function (error) {
	            reject(error);
	        }
	    });
	  });
}

export const postYinLianPay = (data = {}) => {
	// return request.post('/tonglianPayGetSign', data)
	return new Promise((resolve, reject) => {
	    uni.getStorage({
	        key: 'HTTP',
	        success: function (storageRes) {
	            request.post(`${storageRes.data}/yinLianPay`, Object.assign(data))
	                .then(response => {
	                    resolve(response);
	                })
	                .catch(error => {
	                    reject(error);
	                });
	        },
	        fail: function (error) {
	            reject(error);
	        }
	    });
	  });
}


export const postYinLianPayClose = (data = {}) => {
	// return request.post('/tonglianPayGetSign', data)
	return new Promise((resolve, reject) => {
	    uni.getStorage({
	        key: 'HTTP',
	        success: function (storageRes) {
	            request.post(`${storageRes.data}/yinLianPayClose`, Object.assign(data))
	                .then(response => {
	                    resolve(response);
	                })
	                .catch(error => {
	                    reject(error);
	                });
	        },
	        fail: function (error) {
	            reject(error);
	        }
	    });
	  });
}

export const postReturnPay = (data = {}) => {
	// return request.post('/tonglianPayGetSign', data)
	
	return new Promise((resolve, reject) => {
	    uni.getStorage({
	        key: 'HTTP',
	        success: function (storageRes) {
	            request.post(`${storageRes.data}/returnCall`, Object.assign(data))
	                .then(response => {
	                    resolve(response);
	                })
	                .catch(error => {
	                    reject(error);
	                });
	        },
	        fail: function (error) {
	            reject(error);
	        }
	    });
	  });
}




export const postLoginOut = (data = {}) => {
	// return request.post('/doLoginOut', data)
	return new Promise((resolve, reject) => {
	    uni.getStorage({
	        key: 'HTTP',
	        success: function (storageRes) {
	            // 假设 createParams 是一个同步函数，你可以直接传入 storageRes.data
				console.log(storageRes.data);
	            request.post(`${storageRes.data}/doLoginOut`, Object.assign(data))
	                .then(response => {
	                    resolve(response);
	                })
	                .catch(error => {
	                    reject(error);
	                });
	        },
	        fail: function (error) {
	            reject(error);
	        }
	    });
	  });
}

export const postDownload = (data = {}) => {
	// return request.post('/download', data)
	return new Promise((resolve, reject) => {
	    uni.getStorage({
	        key: 'HTTP',
	        success: function (storageRes) {
	            // 假设 createParams 是一个同步函数，你可以直接传入 storageRes.data
				console.log(storageRes.data);
	            request.post(`${storageRes.data}/download`, Object.assign(data))
	                .then(response => {
	                    resolve(response);
	                })
	                .catch(error => {
	                    reject(error);
	                });
	        },
	        fail: function (error) {
	            reject(error);
	        }
	    });
	  });
}


export const postJD = (data = {}) => {
	// return request.post('/tonglianPayGetSign', data)
	
	return new Promise((resolve, reject) => {
	    uni.getStorage({
	        key: 'HTTP',
	        success: function (storageRes) {
	            request.post(`${storageRes.data}/jdbillstate`, Object.assign(data))
	                .then(response => {
	                    resolve(response);
	                })
	                .catch(error => {
	                    reject(error);
	                });
	        },
	        fail: function (error) {
	            reject(error);
	        }
	    });
	  });
}

export const postKDN = (data = {}) => {
	// return request.post('/tonglianPayGetSign', data)
	
	return new Promise((resolve, reject) => {
	    uni.getStorage({
	        key: 'HTTP',
	        success: function (storageRes) {
	            request.post(`${storageRes.data}/kdnbillstate`, Object.assign(data))
	                .then(response => {
	                    resolve(response);
	                })
	                .catch(error => {
	                    reject(error);
	                });
	        },
	        fail: function (error) {
	            reject(error);
	        }
	    });
	  });
}

export const postWXPay = (data = {}) => {
	// return request.post('/tonglianPayGetSign', data)
	return new Promise((resolve, reject) => {
	    uni.getStorage({
	        key: 'HTTP',
	        success: function (storageRes) {
	            request.post(`${storageRes.data}/wechatPayV3`, Object.assign(data))
	                .then(response => {
	                    resolve(response);
	                })
	                .catch(error => {
	                    reject(error);
	                });
	        },
	        fail: function (error) {
	            reject(error);
	        }
	    });
	  });
}

export const postDeleteImage = (data = {}) => {
	// return request.post('/tonglianPayGetSign', data)
	
	return new Promise((resolve, reject) => {
	    uni.getStorage({
	        key: 'HTTP',
	        success: function (storageRes) {
	            request.post(`${storageRes.data}/deleteImage`, Object.assign(data))
	                .then(response => {
	                    resolve(response);
	                })
	                .catch(error => {
	                    reject(error);
	                });
	        },
	        fail: function (error) {
	            reject(error);
	        }
	    });
	  });
}

export const downloadFile = (URL)  => {
	return new Promise((resolve, reject) => {
		uni.getStorage({
			key: 'HTTP',
			success: function (storageRes) {
				uni.request({
					url: `${storageRes.data}/downloadFile?${URL}`, // 使用传入的 URL 构造请求URL
					method: 'GET',
					responseType: 'arraybuffer', // 设置响应数据为arraybuffer，以便处理二进制文件
					success: (res) => {
						// 请求成功，res.data 包含文件的二进制数据
						// 这里可以对文件数据进行处理或者直接返回
						resolve(res); // 返回包含文件数据的响应对象
					},
					fail: (err) => {
						reject(err); // 请求失败，返回错误信息
					}
				});
			},
			fail: function (error) {
				reject(error); // 获取存储失败，返回错误信息
			}
		});
	});
}


// export function getUser() {
//   return http('/api/get');
// }

// export function postCall(params) {
//   return http('/call', {
//     method: 'POST',
//     params: params,
//   });
// }
 
// export function postLogin(params) {
//   return http('/doLogin', {
//     method: 'POST',
//     params: params,
//   });
// }

// export function postLoginOut(params) {
//   return http('/doLoginOut', {
//     method: 'POST',
//     params: params,
//   });
// }

// export function postUserInfo(params) {
//   return http('/doUserInfo', {
//     method: 'POST',
//     params: params,
//   });
// }

// export function postDownload(params) {
//   return http('/download', {
//     method: 'POST',
//     params: params,
//   });
// }

